<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/19
 * Time: 17:56
 */

namespace Game\Services\Client;


use Game\Library\Logger;
use Game\Library\Mysql;
use Game\Traits\Singleton;

class DispatcherClient
{
    use Singleton;
    /**
     * 根据操作码分发操作
     * @param $reqData
     * @return mixed
     */
    public static function dispatcherOpt($reqData)
    {
        Logger::getInstance()->debug($reqData);

        if(isset($reqData['operation']))
        {
            switch($reqData['operation'])
            {
                /*************
                room操作
                 **************/
                case 'CreateRoom':		//创建房间
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->createRoom($reqData);
                    break;
                case 'GuestRoom':		//观战
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->guestRoom($reqData);
                    break;
                case 'JoinRoom':		//进入房间
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->joinRoom($reqData);
                    break;
                case 'ReadyStart':		//用户准备操作
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->readyStart($reqData);
                    break;
                case 'ReadyCancel':		//用户取消准备操作
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->readyCancel($reqData);
                    break;
                case 'PrepareJoinRoom':		//进房之前的查询
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->prepareJoinRoom($reqData);
                    break;
                case 'PullRoomInfo':		//拉取房间信息
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->pullRoomInfo($reqData);
                    break;
                case 'HistoryScoreboard':		//历史积分榜
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->historyScoreboard($reqData);
                    break;
                case 'LastScoreboard':		//历史战绩
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->lastScoreboard($reqData);
                    break;

                case 'TestDouble':		//拉取房间信息
                    $roomOperation = new RoomOperation();
                    $result = $roomOperation->testDouble($reqData);
                    break;


                case 'ClickToLook':		//点击看牌
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->clickToLook($reqData);
                    break;
                case 'ChooseChip':		//选择筹码
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->ChooseChip($reqData);
                    break;

                case 'Discard':		//弃牌
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->discard($reqData);
                    break;
                case 'OpenCard':		//开牌
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->openCard($reqData);
                    break;

                case 'PkCard':		//开牌
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->pkCard($reqData);
                    break;
                case 'BroadcastVoice':		//发送声音
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->broadcastVoice($reqData);
                    break;

                case 'SpeakVoice':		//发送声音2
                    $playerOperation = new PlayerOperation();
                    $result = $playerOperation->speakVoice($reqData);
                    break;

//                case 'LxhServer':		//观察员用于调试收取房间消息
//                    $playerOperation = new PlayerOperation();
//                    $result = $playerOperation->LxhServer($reqData);
//                    break;

                default:
                    $result = OPT_CONST::MISSING_OPERATION;	//数据格式不正确，缺少操作码
                    break;
            }
            //关闭数据库
            @Mysql::getInstance()->closeConnection();
            return $result;
        }
        else
        {
            return OPT_CONST::MISSING_OPERATION;	//缺少操作码
        }
    }


    /**
     *  断开连接
     * @param $array
     * @return bool
     */
    public static function socketClosed($array)
    {
        $roomOperation = new RoomOperation();
        $result = $roomOperation->userDisconnected($array);
        return $result;
    }
}